//
//  LRXPasswordView.m
//  路人侠
//
//  Created by zhangxulong on 3/5/16.
//  Copyright © 2016 zhangxulong. All rights reserved.
//

#import "LRXPasswordView.h"
@interface LRXPasswordView()
@property (nonatomic,weak) UITextField * RePassText;
@property (nonatomic,weak) UITextField * passText;
@end

@implementation LRXPasswordView

+ (instancetype)layoutPasswordView
{
    return [[LRXPasswordView alloc]initPasswordView];
}

- (instancetype)initPasswordView
{
    if (self = [super init]) {
        self.backgroundColor =[UIColor clearColor];
        self.textFiels = [NSMutableArray array];
        CGFloat minX =  37 * wScale;
        CGFloat fontSize = 25 * hScale;
        
        CGFloat viewW = MainScreenW * 0.7;
        CGFloat lineW = viewW - 2 * minX;
        UIView * contentView = [[UIView alloc]init];
        contentView.backgroundColor = [UIColor whiteColor];
        contentView.frame = CGRectMake(112 * wScale,0, viewW, 315 * hScale);
        contentView.layer.cornerRadius = 10 * hScale;
        
    
        
        UITextField * passText = [[UITextField alloc]init]; passText.delegate = self;
        CGFloat passTextY = 75 * hScale;
        passText.placeholder = @"请输入至少6位密码";
        passText.font = [UIFont systemFontOfSize:fontSize];
        passText.frame = CGRectMake(minX, passTextY, lineW, 27 * hScale);
        passText.tag = 2 + 1110;
        passText.returnKeyType = UIReturnKeyNext;
        passText.secureTextEntry = YES;
        self.passText = passText;
        UIImageView * lineView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line"]];
        CGFloat lineViewY = CGRectGetMaxY(passText.frame) + 10 * hScale;
        lineView.frame = CGRectMake(minX, lineViewY, lineW, 2 * hScale);
        
        UITextField * RePassText = [[UITextField alloc]init]; passText.delegate = self;
        CGFloat RePassTextY = lineViewY + 27 * hScale;
        RePassText.placeholder = @"确认秘密";
        RePassText.font = [UIFont systemFontOfSize:fontSize];
        RePassText.frame = CGRectMake(minX, RePassTextY, lineW, 27 * hScale);
        RePassText.tag = 3 + 1110;
        self.RePassText = RePassText;
        RePassText.secureTextEntry = YES;
        
        UIImageView * lineView2 = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"line"]];
        CGFloat lineView2Y = CGRectGetMaxY(RePassText.frame) + 10 * hScale;
        lineView2.frame = CGRectMake(minX, lineView2Y, lineW, 2 * hScale);
        
        
        UIButton * preBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [preBtn setImage:[UIImage imageNamed:@"preBtnImage"] forState:UIControlStateNormal];
        CGFloat preBtnY = lineView2Y + 72 * hScale;
        preBtn.frame = CGRectMake(minX, preBtnY, 30 * wScale, 30 * wScale);
        [preBtn addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
        preBtn.tag = 10000;
        
        UIButton * regBtn = [UIButton buttonWithType:UIButtonTypeSystem];
        [regBtn setTitle:@"注册" forState:UIControlStateNormal];
        [regBtn setTitleColor:mainColor forState:UIControlStateNormal];
        regBtn.titleLabel.font = [UIFont systemFontOfSize:28 * hScale];
        [regBtn addTarget:self action:@selector(changeView:) forControlEvents:UIControlEventTouchUpInside];
        regBtn.tag = 10001;
       
        CGFloat regBtnW = 80 * wScale;
        CGFloat regBtnH = 50 * hScale;
        CGFloat regBtnCenterY = lineView2Y + 68 * hScale ;
        CGFloat regBtnX = CGRectGetMaxX(lineView2.frame) - regBtnW;
        regBtn.frame = CGRectMake(regBtnX, regBtnCenterY, regBtnW, regBtnH);
        
    
        [contentView addSubview:regBtn];
        [contentView addSubview:preBtn];
        [contentView addSubview:passText];
        [contentView addSubview:lineView];
        [contentView addSubview:RePassText];
        [contentView addSubview:lineView2];
        [contentView addSubview:regBtn];
        
        [self.textFiels addObject: passText];
        [self.textFiels addObject: RePassText];
        [self addSubview:contentView];

    }
    return self;
}
/**
 *  向前和向后的按钮
 *
 *  @param btn tag 10000 就是向后
 *  @param btn tag 10001 就是向前
 */
- (void)changeView:(UIButton *)btn
{
    if([self.passwordDelegate respondsToSelector:@selector(changCurView:)]) {
        if (btn.tag == 10000) {
            [self.passwordDelegate changCurView:0];
        }
        if (btn.tag == 10001) {
            
            if (self.passText.text.length < 8) {
                [MBProgressHUD showError:@"请输入至少8位密码"];
            }
            else
            {
                [self reigterBtnTouchInside];            }
        }
    }
}
//注册
- (void)reigterBtnTouchInside
{
    
    if (![self.passText.text isEqualToString:self.RePassText.text]) {
        [MBProgressHUD showError:@"密码不一致"];
    }
    else
    {
        [MBProgressHUD showMessage:@"正在注册"];
        NSDictionary * parmes = @{@"account":self.phoneNum,@"password":self.passText.text};

        AFHTTPSessionManager * manger = [AFHTTPSessionManager manager];
        [manger POST:registerUrl parameters:parmes progress:^(NSProgress * _Nonnull uploadProgress) {
            
        } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
            dispatch_async(dispatch_get_main_queue(), ^{
                [MBProgressHUD hideHUD];
                //调用撤销窗口
                if ([self.passwordDelegate respondsToSelector:@selector(dismissViewController)]) {
                    [self.passwordDelegate dismissViewController];
                }
            });
  
        } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
            [MBProgressHUD hideHUD];
            [MBProgressHUD showError:@"网络连接错误"];
        }];
    }
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [self.RePassText becomeFirstResponder];
    return YES;
}
@end
